package com.cn.longdistancebusstation.welcome;

import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.view.WindowManager;
import android.widget.Toast;

import com.cn.longdistancebusstation.MainActivity;
import com.cn.longdistancebusstation.R;
import com.cn.longdistancebusstation.annotation.LoginStatus;
import com.cn.longdistancebusstation.base.BaseActivity;
import com.cn.longdistancebusstation.global.GlobalVariable;
import com.cn.longdistancebusstation.httpHelper.HttpHelper;
import com.cn.longdistancebusstation.httpapi.HttpService;
import com.cn.longdistancebusstation.model.Result;
import com.cn.longdistancebusstation.model.UserInfo;
import com.google.gson.Gson;

import java.math.BigDecimal;

import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;
import retrofit2.Retrofit;

public class WelcomeActivity extends BaseActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        this.getWindow().setFlags(
                WindowManager.LayoutParams.FLAG_FULLSCREEN,
                WindowManager.LayoutParams.FLAG_FULLSCREEN);
//        requestWindowFeature(Window.FEATURE_NO_TITLE);
        setContentView(R.layout.activity_welcome);

        login();
    }

    private void login() {
        SharedPreferences sharedPreferences = getSharedPreferences("data", MODE_PRIVATE);
        String username = sharedPreferences.getString("telephone", "");
        String pwd = sharedPreferences.getString("password", "");
        if(username.length() == 0 || pwd.length() == 0){
            GlobalVariable.setLoginStatus(LoginStatus.STATUS_LOGOUT);
            Intent intent=new Intent(WelcomeActivity.this,MainActivity.class);
            startActivity(intent);
            finish();
            return;
        }
        showHud("加载中...");
        Retrofit retrofit=new HttpHelper.HttpServiceBuilder().build().getRetrofit();
        HttpService httpService=retrofit.create(HttpService.class);
        Call<Result<UserInfo>> call=httpService.login(username,pwd);
        call.enqueue(new Callback<Result<UserInfo>>() {
            @Override
            public void onResponse(Call<Result<UserInfo>> call, Response<Result<UserInfo>> response) {
                hideHud();
                Result result=response.body();
                Boolean isSuccess =result.getHead().getSuccess();
                UserInfo userInfo=(UserInfo)result.getBody();
                if(isSuccess){
                    String phoneNumber=userInfo.getMobilePhone();
                    String pwd=userInfo.getPassword();
                    String id=userInfo.getId();
                    GlobalVariable.setUserID(id);
                    GlobalVariable.setLoginStatus(LoginStatus.STATUS_LOGIN);
                    //
                    String ticketNumbers=userInfo.getTicketNum();
                    String couponLimit=userInfo.getCoupon();
                    //设置满足接站服务的条件
                    GlobalVariable.setTicketNumForCondition(Integer.parseInt(ticketNumbers));
                    GlobalVariable.setDiscountValueForCondition(new BigDecimal(couponLimit));
                    ////
                }
                else{
                    GlobalVariable.setLoginStatus(LoginStatus.STATUS_LOGOUT);
                }
                Intent intent=new Intent(WelcomeActivity.this,MainActivity.class);
                startActivity(intent);
                finish();
            }

            @Override
            public void onFailure(Call<Result<UserInfo>> call, Throwable t) {
                hideHud();
                Toast.makeText(WelcomeActivity.this, t.getMessage(), Toast.LENGTH_SHORT).show();
            }
        });
    }
}
